package org.raymond.iworks.study.basic.thread.concurrenthashmap;

import java.util.concurrent.ConcurrentHashMap;

public class Demo3 {
    private final ConcurrentHashMap<Integer, Integer> map = new ConcurrentHashMap<>();

    private void update(){
        Integer result = map.get(1);
        if(result==null){
            map.put(1,1);
        }else{
            map.put(1, result+1);
        }
    }

    public void check(){
        System.out.println("assert equals:"+(2==map.get(1).intValue()));
    }

    public static void main(String[] args) throws InterruptedException {
        Demo3 demo3 = new Demo3();
        Thread f = new Thread(()->demo3.update());
        Thread s = new Thread(()->demo3.update());
        f.start();
        s.start();
        f.join();
        s.join();

        demo3.check();
    }
}
